package edu.uwosh.cs342.project4;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class FavoritesList extends Activity {
	
	private ArrayList<String> favoriteNames;
	private ArrayList<Favorite> favoriteList;
	private ListView namesList;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favorite_list);
		
		favoriteNames = new ArrayList<String>();
		favoriteList = new ArrayList<Favorite>();
		DBHelper dbHelper = new DBHelper();
        AsyncMethod<DBHelper> getBitmap = new AsyncMethod<DBHelper>(DBHelper.class, dbHelper, "validate");
        getBitmap.execute();
        String data = (String)getBitmap.getResult();
        //System.out.println(data);
        //Each block of data should be formatted as Latitude, Longitude, Name
        String[] dataBlocks = data.split(",");
        for (int i = 0; i < dataBlocks.length; i++){
        	String[] blockData = dataBlocks[i].split("~");
        	/*for (int j = 0; j < blockData[i].length(); j++){
        		System.out.println(blockData[j]);
        	}*/
        	//System.out.println(blockData[2]);
        	if (blockData.length == 4){
        		favoriteList.add(new Favorite(blockData[0], blockData[1], blockData[2],
        				blockData[3]));
        	}
        	else{
        		favoriteList.add(new Favorite(blockData[0], blockData[1], blockData[2],
        				"p"));
        	}
        		
        	favoriteNames.add(blockData[2]);
        }
		System.out.println("blah");
		namesList = (ListView)findViewById(R.id.nameList);
		namesList.setTextFilterEnabled(true);
		System.out.println("blah");
		namesList.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Favorite temp = favoriteList.get(position);
				
				Intent myIntent = new Intent();
				myIntent.putExtra("latitude", temp.getLatitude());
				myIntent.putExtra("longitude", temp.getLongitude());
				myIntent.putExtra("locationType", temp.getType());
				setResult(Project4Activity.RESULT_FAVORITES, myIntent);
				finish();
			}});
		
		namesList.setAdapter(new ArrayAdapter(this, R.layout.result_list_item, favoriteNames));
		
		Button returnButton = (Button)findViewById(R.id.buttonReturn);
		returnButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});//end cancel button listener
	}//end onCreate

	private class Favorite{
		private String latitude, longitude, name, type;
		
		public Favorite(String latitude, String longitude, String name, String type){
			this.latitude = latitude;
			this.longitude = longitude;
			this.name = name;
		}//end constructor
		
		public String getLatitude(){
			return latitude;
		}//end getLatitude
		
		public String getLongitude(){
			return longitude;
		}//end getLongitude
		
		public String getName(){
			return name;
		}//end getName
		
		public String getType(){
			return type;
		}//end getType
	}//end Favorite class
}//end class
